package com.margo.project.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class FileClient {
	public static void main(String[] args) {
		easydl();
	}
	
	static void easydl() {
		try {
			String url = "http://127.0.0.1:8091/api/easydl/mask";
			//
			CloseableHttpClient httpclient = HttpClients.createDefault();
			//
			HttpPost httpPost = new HttpPost(url);
			File file=new File("F:\\口罩识别\\20200304.jpg");
			FileEntity fileEntity=new FileEntity(file);
			httpPost.setEntity(fileEntity);
			
			HttpResponse response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				String jsonString = EntityUtils.toString(responseEntity, "UTF-8");
				System.out.println(jsonString);
			} else {
				System.out.println("error:" + state + "(" + url + ")");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	/**
	 * POST /spring/iep/mobile/mobileservice/fileAction/upByteFile.iep HTTP/1.1
	 * Content-Length: 5
	 * Host: 127.0.0.1:8080
	 * Connection: Keep-Alive
	 * User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_151)
	 * Accept-Encoding: gzip,deflate
	 * 
	 * 我爱北京天安门
	 */
	static void fileEntity() {
		try {
			String url = "http://127.0.0.1:8080/spring/iep/mobile/mobileservice/fileAction/upByteFile.iep";
			//
			CloseableHttpClient httpclient = HttpClients.createDefault();
			//
			HttpPost httpPost = new HttpPost(url);
			//
			URI uri = MultipartEntityBuilderClient.class.getResource("/file/socket.txt").toURI();
			File file=new File(uri);
			FileEntity fileEntity=new FileEntity(file);
			httpPost.setEntity(fileEntity);
			
			HttpResponse response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				String jsonString = EntityUtils.toString(responseEntity, "UTF-8");
				System.out.println(jsonString);
			} else {
				System.out.println("error:" + state + "(" + url + ")");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	
	/**
	 * 分块编码,如果InputStream没有指定长度，则为分块编码
	 * http报文
	 * POST /spring/iep/mobile/mobileservice/fileAction/upCharFile.iep HTTP/1.1
	 * Transfer-Encoding: chunked
	 * Host: 127.0.0.1:8080
	 * Connection: Keep-Alive
	 * User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_151)
	 * Accept-Encoding: gzip,deflate
	 * 
	 * 5
	 * margo
	 * 0
	 */
	
	static void inputStreamEntity() {
		try {
			String url = "http://127.0.0.1:8080/spring/iep/mobile/mobileservice/fileAction/upCharFile.iep";
			//
			CloseableHttpClient httpclient = HttpClients.createDefault();
			//
			HttpPost httpPost = new HttpPost(url);
			//
			URI uri = MultipartEntityBuilderClient.class.getResource("/file/socket.txt").toURI();
			File file=new File(uri);
			InputStream is=new FileInputStream(file);
			//InputStreamEntity ise=new InputStreamEntity(is,(long)5);
			InputStreamEntity ise=new InputStreamEntity(is);
			httpPost.setEntity(ise);
			HttpResponse response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				String jsonString = EntityUtils.toString(responseEntity, "UTF-8");
				System.out.println(jsonString);
			} else {
				System.out.println("error:" + state + "(" + url + ")");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	/*	
	 * http请求报文
	 * POST /spring/iep/mobile/mobileservice/fileAction/upCharString.iep HTTP/1.1
	 * Content-Length: 14
	 * Content-Type: text/plain; charset=UTF-8
	 * Host: 127.0.0.1:8080
	 * Connection: Keep-Alive
	 * User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_151)
	 * Accept-Encoding: gzip,deflate
	 * 
	 * margo好漂亮
	 */
	static void stringEntity() {
		try {
			String url = "http://127.0.0.1:8080/spring/iep/mobile/mobileservice/fileAction/upCharString.iep";
			//
			CloseableHttpClient httpclient = HttpClients.createDefault();
			//
			HttpPost httpPost = new HttpPost(url);
			//	
			StringEntity stringEntity=new StringEntity("margo好漂亮", "utf-8");
			httpPost.setEntity(stringEntity);
			HttpResponse response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				String jsonString = EntityUtils.toString(responseEntity, "UTF-8");
				System.out.println(jsonString);
			} else {
				System.out.println("error:" + state + "(" + url + ")");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	/**
	 * http报文
	 * POST /spring/iep/mobile/mobileservice/fileAction/urlEncodedFormEntity.iep HTTP/1.1
	 * Content-Length: 11
	 * Content-Type: application/x-www-form-urlencoded
	 * Host: 127.0.0.1:8080
	 * Connection: Keep-Alive
	 * User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_171)
	 * Accept-Encoding: gzip,deflate
	 * 
	 * margo=margo
	 * 
	 * name=%B3%C2%C7%BF&phone=15802705093
	 */
	static void urlEncodedFormEntity() {
		try {
			//String url = "http://127.0.0.1:8080/spring/iep/mobile/mobileservice/fileAction/urlEncodedFormEntity.iep";
			String url = "http://10.10.38.103:8091/api/eim4arel/verifyUserEim";
			//
			CloseableHttpClient httpclient = HttpClients.createDefault();
			//
			HttpPost httpPost = new HttpPost(url);
			//	
			List<NameValuePair> formparams = new ArrayList<NameValuePair>();
			formparams.add(new BasicNameValuePair("name", "陈强"));
			formparams.add(new BasicNameValuePair("phone", "15802705093"));
			UrlEncodedFormEntity urlEncodedFormEntity=new UrlEncodedFormEntity(formparams,Charset.forName("GBK"));
			httpPost.setEntity(urlEncodedFormEntity);
			HttpResponse response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				String jsonString = EntityUtils.toString(responseEntity, "UTF-8");
				System.out.println(jsonString);
			} else {
				System.out.println("error:" + state + "(" + url + ")");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	/**
	 * POST /spring/iep/mobile/mobileservice/fileAction/byteArrayEntity.iep HTTP/1.1
	 * Content-Length: 14
	 * Host: 127.0.0.1:8080
	 * Connection: Keep-Alive
	 * User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_151)
	 * Accept-Encoding: gzip,deflate
	 * 
	 * margo好漂亮
	 */
	static void byteArrayEntity() {
		try {
			String url = "http://127.0.0.1:8080/spring/iep/mobile/mobileservice/fileAction/byteArrayEntity.iep";
			//
			CloseableHttpClient httpclient = HttpClients.createDefault();
			//
			HttpPost httpPost = new HttpPost(url);
			//	
			ByteArrayEntity ByteArrayEntity=new ByteArrayEntity("margo好漂亮".getBytes("UTF-8"));
			httpPost.setEntity(ByteArrayEntity);
			HttpResponse response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				String jsonString = EntityUtils.toString(responseEntity, "UTF-8");
				System.out.println(jsonString);
			} else {
				System.out.println("error:" + state + "(" + url + ")");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	/**
	 * Content has not been provided
	 * basicHttpEntity.setContent(instream)后跟inputStreamEntry类似
	 */
	static void basicHttpEntity() {
		try {
			String url = "http://127.0.0.1:8080/spring/iep/mobile/mobileservice/fileAction/byteArrayEntity.iep";
			//
			CloseableHttpClient httpclient = HttpClients.createDefault();
			//
			HttpPost httpPost = new HttpPost(url);
			//	
			BasicHttpEntity basicHttpEntity=new BasicHttpEntity();
			
//			URI uri = MultipartEntityBuilderClient.class.getResource("/file/socket.txt").toURI();
//			File file=new File(uri);
//			InputStream instream=new FileInputStream(file);
//			basicHttpEntity.setContent(instream);
			
			httpPost.setEntity(basicHttpEntity);
			HttpResponse response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				String jsonString = EntityUtils.toString(responseEntity, "UTF-8");
				System.out.println(jsonString);
			} else {
				System.out.println("error:" + state + "(" + url + ")");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	/**
	 * 跟inputStreamEntry类似
	 * http报文
	 * POST /spring/iep/mobile/mobileservice/fileAction/upCharFile.iep HTTP/1.1
	 * Transfer-Encoding: chunked
	 * Host: 127.0.0.1:8080
	 * Connection: Keep-Alive
	 * User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_151)
	 * Accept-Encoding: gzip,deflate
	 * 
	 * 5
	 * margo
	 * 0
	 */
	static void entityTemplate() {
		try {
			String url = "http://127.0.0.1:8080/spring/iep/mobile/mobileservice/fileAction/entityTemplate.iep";
			//
			CloseableHttpClient httpclient = HttpClients.createDefault();
			//
			HttpPost httpPost = new HttpPost(url);
			URI uri = MultipartEntityBuilderClient.class.getResource("/file/socket.txt").toURI();
			File file=new File(uri);
			ContentProducerImpl contentProducer=new ContentProducerImpl(file);
			EntityTemplate entityTemplate=new EntityTemplate(contentProducer);
			httpPost.setEntity(entityTemplate);
			HttpResponse response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				String jsonString = EntityUtils.toString(responseEntity, "UTF-8");
				System.out.println(jsonString);
			} else {
				System.out.println("error:" + state + "(" + url + ")");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
}
